package cn.btks.commonserver.valid;

import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import java.util.Date;
import java.util.List;

/**
 *
 * @author zhangjing
 * @date 2019/12/13
 * 全局错误消息拦截
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 全局异常捕捉处理 RuntimeException
     *
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = RuntimeException.class)
    public Object errorHandler(RuntimeException ex) {
        logger.info(new Date() + "==" + ex.getMessage());
        if (ex instanceof IllegalArgumentException){
            return new ResultState(StateInfo.Parameter_Passing_Error.getCode(),StateInfo.Parameter_Passing_Error.getMsg()+":" + ex.getMessage());
        }else if (ex instanceof MaxUploadSizeExceededException){
            return new ResultState(StateInfo.Upload_File_Exception.getCode(),StateInfo.Upload_File_Exception.getMsg()+":" + ex.getMessage());
        }
        return new ResultState(StateInfo.DELETE_ERROR_9.getCode(), StateInfo.DELETE_ERROR_9.getMsg() + ":" + ex.getMessage());
    }

    /**
     * 全局处理数据库异常  DataAccessException
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(DataAccessException.class)
    public Object sqlException(DataAccessException e){
        if (e instanceof DuplicateKeyException){
            return new ResultState(StateInfo.Duplicate_Key_Exception.getCode(),StateInfo.Duplicate_Key_Exception.getMsg());
        }else if (e instanceof DataIntegrityViolationException){
            return new ResultState(StateInfo.Data_Integrity_Violation_Exception.getCode(),StateInfo.Data_Integrity_Violation_Exception.getMsg());
        }
        e.printStackTrace();
        return new ResultState(StateInfo.Data_Access_Exception.getCode(),StateInfo.Data_Access_Exception.getMsg());
    }

    /**
     * 全局处理参数校验绑定异常 BindException
     * @param bx
     * @return
     */
    @ResponseBody
    @ExceptionHandler(BindException.class)
    public Object bindException(BindException bx){
        List<ObjectError> errors = bx.getAllErrors();
        ObjectError error = errors.get(0);
        String msg = error.getDefaultMessage();
        return new ResultState(StateInfo.Bind_Exception.getCode(),StateInfo.Bind_Exception.getMsg()+msg);
    }

}
